<?php
// 引用数据库连接文件
require_once 'databaseConnection.php';

// 连接数据库
try {
    // 连接数据库
    $dbConn = connectToDatabase();

    // 获取前端传递的更新数据
    $roomstatus_id = $_POST['roomstatus_id'];
    $updatedStatus = $_POST['status'];
    $updatedCleanliness = $_POST['cleanliness'];
    $updatedCleaner = $_POST['cleaner'];
    $updatedQualified = $_POST['qualified'];
    $updatedSellable = $_POST['sellable'];
    $updatedLastcleaned = $_POST['lastcleaned'];

    // 更新数据库中的数据
    $sql = "UPDATE roomstatus SET status = :status, cleanliness = :cleanliness, cleaner = :cleaner, qualified = :qualified, sellable = :sellable, lastCleaned = :lastcleaned WHERE roomstatus_id = :roomstatus_id";
    $stmt = $dbConn->prepare($sql);
    $stmt->bindParam(':status', $updatedStatus, PDO::PARAM_STR);
    $stmt->bindParam(':cleanliness', $updatedCleanliness, PDO::PARAM_STR);
    $stmt->bindParam(':cleaner', $updatedCleaner, PDO::PARAM_STR);
    $stmt->bindParam(':qualified', $updatedQualified, PDO::PARAM_BOOL); // 注意此处使用 PDO::PARAM_BOOL
    $stmt->bindParam(':sellable', $updatedSellable, PDO::PARAM_BOOL); // 注意此处使用 PDO::PARAM_BOOL
    // $stmt->bindParam(':lastcleaned', $updatedLastcleaned, PDO::PARAM_STR);
    $stmt->bindParam(':lastcleaned', $updatedLastcleaned, PDO::PARAM_STR);
    $stmt->bindParam(':roomstatus_id', $roomstatus_id, PDO::PARAM_INT);
    $stmt->execute();

    // 返回成功状态
    header('Content-Type: application/json');
    echo json_encode(['status' => 'success']);
} catch (PDOException $e) {
    // 返回错误状态
    header('Content-Type: application/json');
    echo json_encode(['status' => 'error', 'message' => '更新数据失败']);
}
